import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'login',
        component: login
    },
    {
        path: '/login',
        name: 'login',
        component: () => import(/* webpackChunkName: "login" */ '../views/login.vue') //懒加载 提高性能
    },
    {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName: "home" */ '../components/homepageView/homepage.vue'),
        meta: {
            title: '主页面',
            role: ['1', '2', '3'] /* 用来控制权限 */
        },
        children: [
            {
                path: '/index',
                name: '系统首页',
                component: () => import(/* webpackChunkName: "index" */'../components/projectView/showAddProject.vue'),
                meta: {
                    title: '系统首页',
                    hideclose: true,
                    role: ['1', '2', '3']
                }

            },
            {
                path: '/header',
                name: 'Header',
                component: () => import(/* webpackChunkName: "home" */ '../components/homepageView/header.vue')
            },
            {
                path: '/footer',
                name: 'Footer',
                component: () => import(/* webpackChunkName: "home" */ '../components/homepageView/footer.vue')
            },
            {
                path: '/aside',
                name: 'Aside',
                component: () => import(/* webpackChunkName: "home" */ '../components/homepageView/aside.vue')
            },
            {
                path: '/tag',
                name: 'tag',
                component: () => import(/* webpackChunkName: "home" */ '../components/homepageView/tag.vue')
            }, {
                path: '/addProject',
                name: 'addProject',
                component: () => import(/* webpackChunkName: "addProject" */ '../components/projectView/addProject.vue'),
                meta: {
                    title: '新增项目',
                    role: ['1', '2', '3']
                }
            }, {
                path: '/addGroup/:id', // 分组
                name: 'addGroup',
                component: () => import(/* webpackChunkName: "addGroup" */ '../components/projectView/addGroup.vue'),
                meta: {
                    title: '项目分组',
                    role: ['1', '2', '3']
                }
            }
            , {
                path: '/testPlan',
                name: 'testPlan',
                component: () => import(/* webpackChunkName: "testPlan" */ '../components/projectView/testPlan.vue'),
                meta: {
                    title: '测试计划',
                    role: ['1', '2', '3']
                }
            }
            , {
                path: '/showReportView',
                name: 'showReportView',
                component: () => import(/* webpackChunkName: "showReportView" */'../components/reportView/showReportView.vue'),
                meta: {
                    title: 'Case结果报表',
                    role: ['1', '2', '3']
                }
            }

            , {
                path: '/addInterfacsList/:id',
                name: 'addInterfacsList',
                component: () => import(/* webpackChunkName: "addInterfacsList" */'../components/interfaceView/addInterfacsList.vue'),
                meta: {
                    title: '接口列表',
                    role: ['1', '2', '3']
                }
            }
            , {
                path: '/publicInterface',
                name: 'publicInterface',
                component: () => import(/* webpackChunkName: "publicInterface" */'../components/interfaceView/publicInterface.vue'),
                meta: {
                    title: '公共接口',
                    role: ['1', '2', '3']
                }
            }
            , {
                path: '/testCaseAction/:id',
                name: 'testCaseAction',
                component: () => import(/* webpackChunkName: "testCaseAction" */'../components/interfaceView/testCaseAction.vue'),
                meta: {
                    title: '测试用例',
                    role: ['1', '2', '3']
                }
            }
            , {
                path: '/addTestCase/:id/:case_id',
                name: 'addTestCase',
                component: () => import(/* webpackChunkName: "addTestCase" */'../components/interfaceView/addTestCase.vue'),
                meta: {
                    title: '新增编辑用例',
                    role: ['1', '2', '3']
                }
            }
            , {
                path: '/userOperate',
                name: 'userOperate',
                component: () => import(/* webpackChunkName: "userOperate" */'../components/systemView/userOperate.vue'),
                meta: {
                    title: '人员配置',
                    role: ['1']
                }
            }
            , {
                path: '/sendEmail',
                name: 'sendEmail',
                component: () => import(/* webpackChunkName: "sendEmail" */'../components/systemView/sendEmail.vue'),
                meta: {
                    title: '邮件配置',
                    role: ['1']
                }
            }]
    }
    , {
        path: '/403',
        name: '403',
        component: () => import('../components/homepageView/403.vue'),
        meta: {title: '当前页面不可访问'}
    }
    ,
     {
        path: '/404',
        name: '404',
        component: () => import('../components/homepageView/404.vue'),
        meta: {title: '路由不存在'}
    },
    {
        path: '*',
        name: '404',
        component: () => import('../components/homepageView/404.vue'),
        meta: {title: '路由不存在'}
    },

]


const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

/**
 * 路由过滤
 */
const rightPathList = ['*', '/', '/login', '/404', '/403'];//直接可以进入的页面
router.beforeEach((to, from, next) => {
    // debugger
    console.log('路由守卫提醒您跳转到:', to.fullPath);
    var token = sessionStorage.getItem('token');
    if (!token && to.path != '/login') {//登陆认证校验,没登录则跳转/login
        next({path: '/login'})
    } else {//权限认证
        if (rightPathList.includes(to.path)) { //直接可以进入的页面
            next();
        } else if (hasPermit(to)) { // 有权限进入
            next();
        } else {
            next('403');
        }

    }
})

//获取当前路由是否有权限访问
function hasPermit(to) {
    var hasPermit = false;
    if (to.meta && to.meta.role) {
        var ruleList = getRuleList();
        hasPermit = ruleList.some(rule => {
            return to.meta.role.includes(rule);
        });
    }
    return hasPermit;
}

//获取登陆的角色集合
function getRuleList() {
    var ruleList = []; //角色数组
    var strRule = sessionStorage.getItem("position");
    if (strRule.indexOf("|") != -1) {
        ruleList = strRule.split("|");
    } else {
        ruleList.push(strRule);
    }

    return ruleList;
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


export default router
